<?php
namespace app\index\controller;
use think\Controller;

class NoteMold extends Index
{
    public function get_one()
    {
        $id = input('param.id',0);
        if( !isset($id) || !$id)
        {
            return ajax_return(302);
        }
        else
        {
            $model = new \app\index\model\NoteMold();
            $model->map['ID'] = $id;
            $model->fields = 'ID,title,intro,show,stars,status,update_time';
            $res = $model->get_one();
            if(!$res)
            {
                return ajax_return(201);
            }
            else
            {
                return ajax_return( 200 , 'successful' , $res);
            }
        }
    }
    public function get_list()
    {
        $params = input('param.');
        $model = new \app\index\model\NoteMold();
        if(isset($params['search']) && !empty($params))
        {
            $model->map[ 'title' ] = [ 'like','%'.$params['search'].'%' ];
        }
        $model->limit = 100;
        $model->fields = 'ID,title,intro,show,status,update_time';
        $res = $model->get_list();
        if(!$res)
        {
            return ajax_return(201);
        }
        else
        {
            return ajax_return( 200 , 'successful' , $res );
        }
    }
    public function add()
    {
        $params = input('param.');
        if( !isset($params['title']) || !$params['title'] )
        {
            return ajax_return(302);
        }
        else
        {
            $params['create_time'] = $params['update_time'] = date('Y-m-d H:i:s');
            $model = new \app\index\model\NoteMold();
            $model->data = $params;
            $res = $model->add();
            if(!$res)
            {
                return ajax_return(500);
            }
            else
            {
                $params['ID'] = $res;
                $params['show'] = 'true';
                return ajax_return(200,'success',$params);
            }
        }
    }
    public function modify()
    {
        $params = input('param.');
        if(!isset($params['ID']) || !isset($params['title']) || !$params['title'])
        {
            return ajax_return(302);
        }
        else
        {
            $params['ID'] = (int)$params['ID'];
            $params['update_time'] = date('Y-m-d H:i:s');
            $model = new \app\index\model\NoteMold();
            $model->map['ID'] = $params['ID'];
            $model->data = $params;
            $res = $model->modify();
            if(!$res)
            {
                return ajax_return(501);
            }
            else
            {
                return ajax_return(200,'successful');
            }
        }
    }
    public function del()
    {
        $id = input('param.id',0);
        $model = new \app\index\model\NoteMold();
        $model->map['ID'] = $id;
        $model->data = [ 'status' => '0' ];
        if( !$model->del() )
        {
            return ajax_return(502);
        }
        else
        {
            return ajax_return(200);
        }
    }
}
